package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author XiangLiang
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Resource
    private DriverInfoFeignClient driverInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private LocationFeignClient locationFeignClient;

    @Resource
    private NewOrderFeignClient newOrderFeignClient;

    /**
     * 小程序授权登录
     *
     * @param code 授权码
     */
    @Override
    public String login(String code) {
        // 远程调用，得到司机id
        Result<Long> info = driverInfoFeignClient.login(code);
        Long driverId = info.getData();

        // TODO 判断
        if (driverId == null) {
            throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
        }

        // 生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        // 放到redis，设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                driverId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);

        return token;
    }

    /**
     * 获取司机认证信息
     *
     * @param driverId 司机id
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> driverAuthInfoVoResult = driverInfoFeignClient.getDriverAuthInfo(driverId);

        return driverAuthInfoVoResult.getData();
    }

    /**
     * 更新司机认证信息
     *
     * @param updateDriverAuthInfoForm 更新司机认证信息VO
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.updateDriverAuthInfo(updateDriverAuthInfoForm);

        return booleanResult.getData();
    }

    /**
     * 创建司机人脸模型
     *
     * @param driverFaceModelForm 司机人脸模型VO
     * @return 创建结果
     */
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);

        return booleanResult.getData();
    }

    /**
     * 判断司机当日是否进行过人脸识别
     *
     * @param driverId 司机Id
     */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    /**
     * 验证司机人脸
     *
     * @param driverFaceModelForm 司机人脸模型VO
     */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    /**
     * 开始接单服务
     *
     * @param driverId 司机Id
     */
    @Override
    public Boolean startService(Long driverId) {
        // 判断认证状态
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverInfo(driverId).getData();
        if (driverLoginVo.getAuthStatus() != 2) {
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }

        // 判断当日是否人脸识别
        Boolean isFaceRecognition = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if (!isFaceRecognition) {
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }

        // 更新司机接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 1);

        // 删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        // 清空司机新订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }

    /**
     * 停止接单服务
     *
     * @param driverId 司机Id
     */
    @Override
    public Boolean stopService(Long driverId) {
        // 更新司机接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 0);

        // 删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        // 清空司机新订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }
}
